#!/usr/bin/python3
# -*- coding: utf-8 -*-


import os, sys
import yaml
from aiohttp import web

routes = web.RouteTableDef()

def init_func(argv):
    app = web.Application()
    # app.router.add_get("/", v)
    # routes.static('/js', 'js')
    # routes.static('/img', 'img')
    routes.static('/view', 'html')
    # routes.static('/video', 'video')
    # routes.static('/audio', 'audio')
    # routes.static('/model', 'model')
    app.add_routes(routes)
    return app
    
@routes.get('/')
async def get_handler(request):
    route = [item if '.' not in item else item.split('.', 1)[0] for item in os.listdir('html') if item.endswith('.html')]
    data = {
        'msg': "Hello",
        'routes': ['%s/view/%s.html' % (config.get('url', 'http://127.0.0.1:8080'), item) for item in route]
        }
    return web.json_response(data)

config = {}

def get_config():
    # with open(os.path.join(os.path.dirname(__file__), ".config.yml")) as f:
    if not os.path.exists(".config.yml"): return
    with open(".config.yml") as f:
        global config
        conf = yaml.safe_load(f)
        config = dict(config, **conf)

if __name__ == '__main__':
    get_config()
    web.run_app(init_func('argv'),host="0.0.0.0", port=8080)
